package com.syf.kit.core.dao.impl;

import com.syf.kit.cont.RdsDSCont;
import com.syf.kit.core.dao.BaseDTO;
import com.syf.kit.core.dao.SyfRdsDaoUtil;
import com.syf.kit.core.dao.mapper.SyfMapper;
import com.syf.kit.core.dao.sql.JdbcSQLProcessor;

import java.util.HashMap;
import java.util.Map;

public class SyfMultipleRdsDaoUtil implements SyfRdsDaoUtil {
    /**
     * 多数据源，一个数据源对应一个SimpleRdsDaoManager.其中Key：配置的数据源名，value对应数据源建立的SimpleRdsDaoManager
     */
    private final Map<String, SyfSimpleRdsDaoUtil> daoManagerMap = new HashMap<>();

    /**
     * 默认的数据源
     */
    private final SyfSimpleRdsDaoUtil defaultDaoManager;

    public SyfMultipleRdsDaoUtil(Map<String, SyfSimpleRdsDaoUtil> daoManagerMap) {
        this.defaultDaoManager = daoManagerMap.get(RdsDSCont.DEFAULT_DS_NAME);
        if (defaultDaoManager == null) {
            throw new RuntimeException("多数据源中，必须指定默认的数据源，默认数据源的名称为" + RdsDSCont.DEFAULT_DS_NAME);
        }
        this.daoManagerMap.putAll(daoManagerMap);
    }

    @Override
    public JdbcSQLProcessor getSQLProcessor() {
        return defaultDaoManager.getSQLProcessor();
    }

    @Override
    public <T extends BaseDTO> SyfMapper<T> getMapper(Class<T> dtoClass) {
        return defaultDaoManager.getMapper(dtoClass);
    }

    @Override
    public void openConnection() {
        defaultDaoManager.openConnection();
    }

    /**
     * 多数据源的关闭方法，需要关闭所有的DataSourceManager
     */
    @Override
    public void close() {
        for (SyfSimpleRdsDaoUtil daoManager : daoManagerMap.values()) {
            daoManager.close();
        }
    }

    @Override
    public void beginTransaction() {
        defaultDaoManager.beginTransaction();
    }

    @Override
    public void closeTransaction() {
        defaultDaoManager.closeTransaction();
    }

    @Override
    public void commitTransaction() {
        defaultDaoManager.commitTransaction();
    }

    @Override
    public void rollbackTransaction() {
        defaultDaoManager.commitTransaction();
    }

    // --------指定dsName---------

    @Override
    public JdbcSQLProcessor getSQLProcessor(String dsName) {
        return daoManagerMap.get(dsName).getSQLProcessor(dsName);
    }

    @Override
    public <T extends BaseDTO> SyfMapper<T> getMapper(String dsName, Class<T> dtoClass) {
        return daoManagerMap.get(dsName).getMapper(dsName, dtoClass);
    }

    @Override
    public void openConnection(String dsName) {
        daoManagerMap.get(dsName).openConnection(dsName);
    }

    @Override
    public void close(String dsName) {
        daoManagerMap.get(dsName).close(dsName);
    }

    @Override
    public void beginTransaction(String dsName) {
        daoManagerMap.get(dsName).beginTransaction(dsName);
    }

    @Override
    public void closeTransaction(String dsName) {
        daoManagerMap.get(dsName).closeTransaction(dsName);
    }

    @Override
    public void commitTransaction(String dsName) {
        daoManagerMap.get(dsName).commitTransaction(dsName);
    }

    @Override
    public void rollbackTransaction(String dsName) {
        daoManagerMap.get(dsName).rollbackTransaction(dsName);
    }

    @Override
    public void releaseResource() {
        for (SyfSimpleRdsDaoUtil simpleRdsDaoUtil : daoManagerMap.values()) {
            simpleRdsDaoUtil.close();
        }
    }
}
